//
//  HomeViewController.m
//  jungoo
//
//  Created by Dang Quang Trung on 7/25/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "HomeViewController.h"
#import "MyProfileViewController.h"
#import "PeopleViewController.h"
#import "OffersViewController.h"
#import "EventViewController.h"
#import "MessageViewController.h"
#import "MessageNotifyCenter.h"

@implementation HomeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)asyncLoadAvatar
{    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
    
    dispatch_async(queue, ^{
        NSDictionary *userInfo = [JGoUtility sharedCreateAccount];
        NSError *error = nil;                              
        UIImage *personAvatar = [JGoAPI downloadAvatar:[userInfo objectForKey:@"thumb"] error:&error];
        
        if (!error) {
            [userInfo setValue:personAvatar forKey:@"avatar"];
        }
    });       
}

#pragma mark - View lifecycle
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"home_bg.png"]]];
    [JGoUtility setSharedRootViewController:self];
    [self asyncLoadAvatar];
    [MessageNotifyCenter sharedMessageNotify];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark IBAction

-(IBAction)profilePressed:(id)sender{
    MyProfileViewController *profileView = [[MyProfileViewController alloc]initWithNibName:@"MyProfileViewController" bundle:nil];
    [profileView setMyProfileMode:EditMode];

    [self.navigationController pushViewController:profileView animated:YES];
    [profileView release];
}

-(IBAction)peoplePressed:(id)sender{
    PeopleViewController *pVC = [[PeopleViewController alloc]initWithNibName:@"PeopleViewController" bundle:nil];
    pVC.isFav = NO;
    pVC.peopleView = PeopleMode;
    [self.navigationController pushViewController:pVC animated:YES];
    [pVC release];
}

-(IBAction)offerPressed:(id)sender
{
    OffersViewController *oVC = [[OffersViewController alloc]initWithNibName:@"OffersViewController" bundle:nil];
    [self.navigationController pushViewController:oVC animated:YES];
    [oVC release];
}

-(IBAction)favoritePressed:(id)sender
{
    PeopleViewController *pVC = [[PeopleViewController alloc]initWithNibName:@"PeopleViewController" bundle:nil];
    pVC.isFav = YES;
    pVC.peopleView = PeopleMode;
    [self.navigationController pushViewController:pVC animated:YES];
    [pVC release];
}

- (IBAction)eventPressed:(id)sender
{
    EventViewController *eVC = [[EventViewController alloc]initWithNibName:@"EventViewController" bundle:nil];
    [self.navigationController pushViewController:eVC animated:YES];
    [eVC release];    
}

- (IBAction)message:(id)sender
{
    MessageViewController *msgVC = [[[MessageViewController alloc] initWithNibName:@"MessageViewController" 
                                                                            bundle:nil] autorelease];
    
    [self.navigationController pushViewController:msgVC animated:YES];
}

@end
